import { isString, isDate } from '@/utils/typeUtil.js';

export const formatDate = (date) => {
  let YYYY = null;
  let M = null;
  let MM = null;
  let D = null;
  let DD = null;
  let h = null;
  let hh = null;
  let m = null;
  let mm = null;
  let s = null;
  let ss = null;
  let ms = null;
  let ms2 = null;
  let ms3 = null;
  let ms4 = null;
  let dt = null;

  // 如果 date 是 String 类型
  if (date && isString(date)) {
    // 真机运行时，如果直接用 new Date('YYYY-MM-DD hh:mm:ss') 会报 Invalid Date 错误，所以采用下面的方式创建日期
    let dtArr = date
      .replace(/\//g, '.')
      .replace(/-/g, '.')
      .replace(/:/g, '.')
      .replace(/T/g, ' ')
      .replace(' ', '.')
      .replace('Z', '')
      .split('.');

    let year = new Date().getFullYear();
    let month = new Date().getMonth();
    let day = new Date().getDate();
    let hour = new Date().getHours();
    let minute = new Date().getMinutes();
    let second = new Date().getSeconds();
    let millisecond = new Date().getMinutes();

    // 年
    if (dtArr.length > 0 && !isNaN(dtArr[0])) {
      year = parseInt(dtArr[0]);
    }
    // 月
    if (dtArr.length > 1 && !isNaN(dtArr[1])) {
      month = parseInt(dtArr[1]);
    }
    // 日
    if (dtArr.length > 2 && !isNaN(dtArr[2])) {
      day = parseInt(dtArr[2]);
    }
    // 时
    if (dtArr.length > 3 && !isNaN(dtArr[3])) {
      hour = parseInt(dtArr[3]);
    }
    // 分
    if (dtArr.length > 4 && !isNaN(dtArr[4])) {
      minute = parseInt(dtArr[4]);
    }
    // 秒
    if (dtArr.length > 5 && !isNaN(dtArr[5])) {
      second = parseInt(dtArr[5]);
    }
    // 毫秒
    if (dtArr.length > 6 && !isNaN(dtArr[6])) {
      millisecond = parseInt(dtArr[6]);
    }

    date = new Date(year, month - 1, day, hour, minute, second, millisecond);
  }

  // 如果 date 是 Date 类型
  if (date && isDate(date)) {
    YYYY = date.getFullYear();
    M = date.getMonth() + 1;
    MM = M >= 10 ? M : '0' + M;
    D = date.getDate();
    DD = D >= 10 ? D : '0' + D;
    h = date.getHours();
    hh = h >= 10 ? h : '0' + h;
    m = date.getMinutes();
    mm = m >= 10 ? m : '0' + m;
    s = date.getSeconds();
    ss = s >= 10 ? s : '0' + s;
    ms = date.getMilliseconds();
    ms2 = ms;
    ms3 = ms;
    ms4 = ms;
    if (ms < 10) {
      ms2 = '0' + ms;
      ms3 = '00' + ms;
      ms4 = '000' + ms;
    } else if (ms < 100) {
      ms3 = '0' + ms;
      ms4 = '00' + ms;
    } else {
      ms4 = '0' + ms;
    }
  }

  // 返回的数据对象
  const result = {
    YYYY: YYYY,
    MM: MM,
    M: M,
    DD: DD,
    D: D,
    hh: hh,
    h: h,
    mm: mm,
    m: m,
    ss: ss,
    s: s,
    ms: ms,
    ms2: ms2,
    ms3: ms3,
    ms4: ms4,
    dt: date,
    f1: `${YYYY}-${MM}-${DD}`,
    f2: `${YYYY}年${M}月${D}日`,
    f3: `${YYYY}-${M}-${D} ${hh}:${mm}`,
    f4: `${h}:${m}:${s}`,
    f5: `${MM}-${DD}`,
    f6: `${YYYY}-${MM}`,
    f7: `${YYYY}年${M}月`,
    f8: `${h}:${m}`,
    f9: `${M}月${D}日`,
    notes:
      'YYYY（年），MM（月，补0），M（月，不补0），DD（日，补0），D（日，不补0），hh（时，补0），h（时，不补0），mm（分，补0），m（分，不补0），ss（秒，补0），s（秒，不补0），ms（毫秒，不补0），ms2（毫秒，补0到2位），ms3（毫秒，补0到3位），ms4（毫秒，补0到4位），其余的f1，f2，... 看格式就知道了！',
  };
  return result;
};

export const formatDateByFmt = (dateString, fmt) => {
  let date = new Date(dateString);

  if (/(y+)/.test(fmt) || /(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str));
    }
  }
  return fmt;
};

export const formatDateByFmt2 = (date, fmt) => {
  if (/(y+)/.test(fmt) || /(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str));
    }
  }
  return fmt;
};

export const getWeek = (dateString) => {
  let str = '';
  let date = new Date(dateString);
  let week = date.getDay();
  if (week == 0) {
    str = '星期日';
  } else if (week == 1) {
    str = '星期一';
  } else if (week == 2) {
    str = '星期二';
  } else if (week == 3) {
    str = '星期三';
  } else if (week == 4) {
    str = '星期四';
  } else if (week == 5) {
    str = '星期五';
  } else if (week == 6) {
    str = '星期六';
  }
  return str;
};

export const padLeftZero = (str) => {
  return ('00' + str).substr(str.length);
};

export const getCurMonth = () => {
  const date = new Date();
  const month = date.getMonth() + 1;
  return month;
};

export const getCurYear = () => {
  const date = new Date();
  const year = date.getFullYear();
  return year;
};

export const getMonthBegin = (year, month) => {
  let dd = '01';
  let MM = '';
  if (month > 9) {
    MM = month + '';
  } else {
    MM = '0' + month;
  }
  return year + '-' + MM + '-' + dd;
};

export const getMonthEnd = (year, month) => {
  let dd = '';
  let MM = '';
  if (
    month === 1 ||
    month === 3 ||
    month === 5 ||
    month === 7 ||
    month === 8 ||
    month === 10 ||
    month === 12
  ) {
    dd = '31';
  }
  if (month === 4 || month === 6 || month === 9 || month === 11) {
    dd = '30';
  }
  if (month === 2) {
    if (year % 4 === 0) {
      dd = '29';
    } else {
      dd = '28';
    }
  }
  if (month > 9) {
    MM = month + '';
  } else {
    MM = '0' + month;
  }
  return year + '-' + MM + '-' + dd;
};

export const getFriendlyDate = (createTime) => {
  if(createTime) {
    createTime =createTime.replace(new RegExp('-','g'), '/')
  } else {
    return ''
  }
  let tt = new Date(createTime);
  let days = parseInt((new Date().getTime() - tt.getTime()) / 86400000);
  let today = new Date().getDate();
  let year = tt.getFullYear();
  let mouth = tt.getMonth() + 1;
  let day = tt.getDate();
  let time = tt.getHours() < 10 ? '0' + tt.getHours() : tt.getHours();
  let min = tt.getMinutes() < 10 ? '0' + tt.getMinutes() : tt.getMinutes();
  let result, offset;
  offset = Math.abs(today - day);
  if (days < 4 && offset < 3) {
    if (offset === 0) {
      result = '今天 ' + time + ':' + min;
    } else if (offset === 1) {
      result = '昨天 ' + time + ':' + min;
    } else if (offset === 2) {
      result = '前天 ' + time + ':' + min;
    }
  } else {
    if (mouth < 10) {
      mouth = '0' + mouth;
    }
    result = year + '-' + mouth + '-' + day + ' ' + time + ':' + min;
  }
  return result;
};